#include "terrianGLSL.h"

#include <cmath>
#include <iostream>
#include <strstream>

using namespace std;

//普通纹理贴图
void shaderCode1(std::ostrstream& theVS, std::ostrstream& thePS)
{
	theVS<<"uniform float crate;"<<endl;
	theVS<<"varying float prate;"<<endl;

	theVS<<"varying float xpos;"<<endl;
	theVS<<"varying float ypos;"<<endl;

	//theVS<<"varying vec2 texcoods;"<<endl; 

	theVS<<"void main()"<<endl;
	theVS<<"{"<<endl;

	theVS<<"	prate = 1.0-crate;"<<endl;	
	theVS<<"	gl_FrontColor = gl_Color;"<<endl;
	theVS<<"	gl_Position = ftransform();"<<endl;
	theVS<<"	xpos = gl_Position.x;"<<endl;
	theVS<<"	ypos = gl_Position.y;"<<endl;

	//theVS<<"	texcoods = noise2(vec2(0.5, 0.5));"<<endl;


	theVS<<"	gl_FrontColor.r = gl_FrontColor.r*crate + gl_Position.x*prate;"<<endl;
	theVS<<"	gl_FrontColor.g = gl_FrontColor.g*crate + gl_Position.y*prate;"<<endl;
	theVS<<"	gl_FrontColor.b = gl_FrontColor.b*crate + gl_Position.z*prate;"<<endl;	

	theVS<<"	gl_TexCoord[0] = gl_MultiTexCoord0;"<<endl;	
	theVS<<"}"<<endl<<ends;

	//------------------------------
	thePS<<"uniform sampler2D testtex;"<<endl;
	thePS<<"varying float xpos;"<<endl;
	thePS<<"varying float ypos;"<<endl;

	thePS<<"void main()"<<endl;
	thePS<<"{"<<endl;
	//thePS<<"	gl_FragColor = gl_Color;"<<endl;	
	//thePS<<"	float xxpos = xpos*256.0;"<<endl;
	//thePS<<"	float yypos = ypos*256.0;"<<endl;
	
	//thePS<<"	gl_FragColor = texture2D(testtex, texcoods);"<<endl;	
	thePS<<"	gl_FragColor = texture2D(testtex, vec2(sin(xpos), cos(ypos)));"<<endl;	
	//thePS<<"	gl_FragColor = texture2D(testtex, gl_TexCoord[0].xy);"<<endl;	
	thePS<<"	if(gl_FragColor.r<0.1 && gl_FragColor.b<0.1 && gl_FragColor.b<0.1)"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		gl_FragColor = gl_Color;"<<endl;
	thePS<<"	}"<<endl;
	thePS<<"}"<<endl<<ends;
}

//cub map效果
void shaderCode2(std::ostrstream& theVS, std::ostrstream& thePS)
{
	theVS<<"uniform float crate;"<<endl;
	theVS<<"varying float prate;"<<endl;
	theVS<<"varying vec3 eyedrc;"<<endl;

	theVS<<"void main()"<<endl;
	theVS<<"{"<<endl;

	theVS<<"	prate = 1.0-crate;"<<endl;	
	theVS<<"	gl_FrontColor = gl_Color;"<<endl;
	theVS<<"	gl_Position = ftransform();"<<endl;

	//theVS<<"	gl_FrontColor.r = gl_FrontColor.r*crate + gl_Position.x*prate;"<<endl;
	//theVS<<"	gl_FrontColor.g = gl_FrontColor.g*crate + gl_Position.y*prate;"<<endl;
	//theVS<<"	gl_FrontColor.b = gl_FrontColor.b*crate + gl_Position.z*prate;"<<endl;	
	theVS<<"	gl_FrontColor.b = gl_FrontColor.b*crate + gl_Position.z*prate;"<<endl;	

	theVS<<"}"<<endl<<ends;

	//------------------------------
	thePS<<"void main()"<<endl;
	thePS<<"{"<<endl;
	thePS<<"	gl_FragColor = gl_Color;"<<endl;	
	thePS<<"}"<<endl<<ends;
}

void shaderCodeMandelbrotSet(std::ostrstream& theVS, std::ostrstream& thePS)
{	

	theVS<<"varying vec2 xypos;"<<endl;

	theVS<<"void main()"<<endl;
	theVS<<"{"<<endl;	
	theVS<<"	gl_FrontColor = gl_Color;"<<endl;
	theVS<<"	xypos = gl_Vertex.yz;"<<endl;	
	theVS<<"	gl_Position = ftransform();"<<endl;
	theVS<<"}"<<endl<<ends;

	//------------------------------

	thePS<<"varying vec2 xypos;"<<endl;

	thePS<<"uniform float zoom;"<<endl;

	thePS<<"uniform float xcen;"<<endl;
	thePS<<"uniform float ycen;"<<endl;

	thePS<<"uniform float inix;"<<endl;
	thePS<<"uniform float iniy;"<<endl;

	thePS<<"uniform float maxforNum;"<<endl;	

	thePS<<"void main()"<<endl;
	thePS<<"{"<<endl;
	thePS<<"	float lenToO=0.0;"<<endl;
	thePS<<"	float computeCount=0.0;"<<endl;	
	//thePS<<"	float thereal=0.0;"<<endl;
	//thePS<<"	float theimg=0.0;"<<endl;
	thePS<<"	float thereal	=	xypos.x*zoom+xcen;"<<endl;
	thePS<<"	float theimg	=	xypos.y*zoom+ycen;"<<endl;

	thePS<<"	float Creal=inix;"<<endl;
	thePS<<"	float Cimg=iniy;"<<endl;

	thePS<<"	float tmpreal;"<<endl;

	thePS<<"	for(; lenToO<4.0 && computeCount<maxforNum; computeCount=computeCount+1.0)"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		tmpreal = thereal;"<<endl;
	
	thePS<<"		thereal = tmpreal*tmpreal-theimg*theimg + Creal;"<<endl;
	thePS<<"		theimg = 2.0*tmpreal*theimg + Cimg;"<<endl;

	thePS<<"		lenToO = thereal*thereal + theimg*theimg;"<<endl;
	thePS<<"	}"<<endl;
	
	thePS<<"	if(lenToO>4.0)"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		computeCount = computeCount/21.0;"<<endl;
	thePS<<"		gl_FragColor = vec4(computeCount, 0.0, 1.0-computeCount, 1.0);"<<endl;
	//thePS<<"		gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);"<<endl;
	thePS<<"	}"<<endl;
	thePS<<"	else"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);"<<endl;
	thePS<<"	}"<<endl;
	thePS<<"}"<<endl<<ends;
}

bool shaderSetMandelbrotSet()
{
	std::ostrstream theVS;	
	std::ostrstream thePS;

	shaderCodeMandelbrotSet(theVS, thePS);

	//1 of  3 in compile stage	
	AppGloble::theVShader = glCreateShader(GL_VERTEX_SHADER);
	AppGloble::thePShader = glCreateShader(GL_FRAGMENT_SHADER);


	const char * vv = theVS.str();
	const char * pp = thePS.str();

	//2 of  3 in compile stage
	glShaderSource(AppGloble::theVShader, 1, &vv, NULL);
	glShaderSource(AppGloble::thePShader, 1, &pp, NULL);

	//3 of  3 in compile stage
	glCompileShader(AppGloble::theVShader);

	int param;
	glGetShaderiv(AppGloble::theVShader, GL_COMPILE_STATUS, &param); 

	if(param!=GL_TRUE)
	{
		cout<<endl<<"fail to compile vs shader"<<endl;
		cout<<endl<<"the shader is:"<<endl<<theVS.str()<<endl;

		char errorInf[1024*5] = {'\0'};
		int theErrorReportLen;
		glGetShaderInfoLog(AppGloble::theVShader, 1024*5, &theErrorReportLen, errorInf); 

		cout<<endl<<"the Error Report is:"<<endl<<errorInf<<endl;

		return false;
	}

	glCompileShader(AppGloble::thePShader);

	glGetShaderiv(AppGloble::thePShader, GL_COMPILE_STATUS, &param); 
	if(param!=GL_TRUE)
	{
		cout<<endl<<"fail to compile ps shader"<<endl;
		cout<<endl<<"the shader is:"<<endl<<thePS.str();

		char errorInf[1024*5] = {'\0'};
		int theErrorReportLen;
		glGetShaderInfoLog(AppGloble::thePShader, 1024*5, &theErrorReportLen, errorInf); 

		cout<<endl<<"the Error Report is:"<<endl<<errorInf<<endl;

		return false;
	}

	//1 of  3 in link stage
	AppGloble::prgrm = glCreateProgram();

	//2 of  3 in link stage
	glAttachShader(AppGloble::prgrm, AppGloble::theVShader);
	glAttachShader(AppGloble::prgrm, AppGloble::thePShader);	

	//3 of  3 in link stage
	glLinkProgram(AppGloble::prgrm);

	glGetProgramiv(AppGloble::prgrm, GL_LINK_STATUS, &param); 		
	if(param!=GL_TRUE)
	{
		cout<<endl<<"fail to link shader code"<<endl;	

		char errorInf[1024*5] = {'\0'};
		int theErrorReportLen;
		glGetProgramInfoLog(AppGloble::prgrm, 1024*5, &theErrorReportLen, errorInf); 

		cout<<endl<<"the Error Report is:"<<endl<<errorInf<<endl;

		return false;
	}

	//finally after this call render line use my glsl

	glUseProgram(AppGloble::prgrm);
	AppGloble::MST_zoomLOC = glGetUniformLocation(AppGloble::prgrm, "zoom"); 
	glUniform1f(AppGloble::MST_zoomLOC, AppGloble::MST_zoom);

	AppGloble::MST_xcenLOC = glGetUniformLocation(AppGloble::prgrm, "xcen"); 
	glUniform1f(AppGloble::MST_xcenLOC, AppGloble::MST_xcen);

	AppGloble::MST_ycenLOC = glGetUniformLocation(AppGloble::prgrm, "ycen"); 
	glUniform1f(AppGloble::MST_ycenLOC, AppGloble::MST_ycen);

	AppGloble::MST_inixLOC = glGetUniformLocation(AppGloble::prgrm, "inix"); 
	glUniform1f(AppGloble::MST_inixLOC, AppGloble::MST_inix);

	AppGloble::MST_iniyLOC = glGetUniformLocation(AppGloble::prgrm, "iniy"); 
	glUniform1f(AppGloble::MST_iniyLOC, AppGloble::MST_iniy);

	AppGloble::MST_maxforNumLOC = glGetUniformLocation(AppGloble::prgrm, "maxforNum"); 
	glUniform1f(AppGloble::MST_maxforNumLOC, AppGloble::MST_maxforNum);

	return true;
}


bool shaderSet()
{
	std::ostrstream theVS;	
	std::ostrstream thePS;

	shaderCode1(theVS, thePS);

	//1 of  3 in compile stage
	//AppGloble::theVShader = glCreateShaderObjectARB(GL_VERTEX_SHADER);
	//AppGloble::thePShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER);
	
	AppGloble::theVShader = glCreateShader(GL_VERTEX_SHADER);
	AppGloble::thePShader = glCreateShader(GL_FRAGMENT_SHADER);


	const char * vv = theVS.str();
	const char * pp = thePS.str();

	//2 of  3 in compile stage
	glShaderSource(AppGloble::theVShader, 1, &vv, NULL);
	glShaderSource(AppGloble::thePShader, 1, &pp, NULL);

	//3 of  3 in compile stage
	glCompileShader(AppGloble::theVShader);

	int param;
	glGetShaderiv(AppGloble::theVShader, GL_COMPILE_STATUS, &param); 
	
	if(param!=GL_TRUE)
	{
		cout<<endl<<"fail to compile vs shader"<<endl;
		cout<<endl<<"the shader is:"<<endl<<theVS.str()<<endl;

		char errorInf[1024*5] = {'\0'};
		int theErrorReportLen;
		glGetShaderInfoLog(AppGloble::theVShader, 1024*5, &theErrorReportLen, errorInf); 

		cout<<endl<<"the Error Report is:"<<endl<<errorInf<<endl;

		return false;
	}

	glCompileShader(AppGloble::thePShader);

	glGetShaderiv(AppGloble::thePShader, GL_COMPILE_STATUS, &param); 
	if(param!=GL_TRUE)
	{
		cout<<endl<<"fail to compile ps shader"<<endl;
		cout<<endl<<"the shader is:"<<endl<<thePS.str();

		char errorInf[1024*5] = {'\0'};
		int theErrorReportLen;
		glGetShaderInfoLog(AppGloble::thePShader, 1024*5, &theErrorReportLen, errorInf); 

		cout<<endl<<"the Error Report is:"<<endl<<errorInf<<endl;

		return false;
	}

	//1 of  3 in link stage
	AppGloble::prgrm = glCreateProgram();

	//2 of  3 in link stage
	glAttachShader(AppGloble::prgrm, AppGloble::theVShader);
	glAttachShader(AppGloble::prgrm, AppGloble::thePShader);	

	//3 of  3 in link stage
	glLinkProgram(AppGloble::prgrm);

	glGetProgramiv(AppGloble::prgrm, GL_LINK_STATUS, &param); 	
	//object	- the handler to the object. Either a shader or a program 
	//type		- GL_LINK_STATUS. 
	//param		- the return value, GL_TRUE if OK, GL_FALSE otherwise. 

	if(param!=GL_TRUE)
	{
		cout<<endl<<"fail to link shader code"<<endl;	

		char errorInf[1024*5] = {'\0'};
		int theErrorReportLen;
		glGetProgramInfoLog(AppGloble::prgrm, 1024*5, &theErrorReportLen, errorInf); 

		cout<<endl<<"the Error Report is:"<<endl<<errorInf<<endl;

		return false;
	}

	//finally after this call render line use my glsl
	//glUseProgram(prgrm);
	AppGloble::crateLoc = glGetUniformLocation(AppGloble::prgrm, "crate"); 
	if(param!=GL_TRUE)
	{
		cout<<endl<<"fail to link shader code"<<endl;	

		char errorInf[1024*5] = {'\0'};
		int theErrorReportLen;
		glGetProgramInfoLog(AppGloble::prgrm, 1024*5, &theErrorReportLen, errorInf); 

		cout<<endl<<"the Error Report is:"<<endl<<errorInf<<endl;

		return false;
	}

	glUniform1f(AppGloble::crateLoc, AppGloble::crateUniformShader);

	AppGloble::colorTexloc = glGetUniformLocation(AppGloble::prgrm, "testtex"); 
	if(param!=GL_TRUE)
	{
		cout<<endl<<"fail to link shader code"<<endl;	

		char errorInf[1024*5] = {'\0'};
		int theErrorReportLen;
		glGetProgramInfoLog(AppGloble::prgrm, 1024*5, &theErrorReportLen, errorInf); 

		cout<<endl<<"the Error Report is:"<<endl<<errorInf<<endl;

		return false;
	}

	glUniform1i(AppGloble::colorTexloc, AppGloble::textureNUM);

	return true;
}

void shaderCode_terrain(std::ostrstream& theVS, std::ostrstream& thePS)
{
	//theVS<<"uniform sampler2D terraintex;"<<endl;
	//theVS<<"uniform float crate;"<<endl;
	//theVS<<"varying float prate;"<<endl;

	theVS<<"varying float xpos;"<<endl;
	theVS<<"varying float ypos;"<<endl;

	//theVS<<"varying vec2 texcoods;"<<endl; 

	theVS<<"void main()"<<endl;
	theVS<<"{"<<endl;
	theVS<<"	gl_FrontColor = gl_Color;"<<endl;
	
	//theVS<<"	vec2 trnTexCoord = vec2(gl_Vertex.x, gl_Vertex.y);"<<endl;	
	//theVS<<"	normalize(trnTexCoord);"<<endl;
	//theVS<<"	gl_FrontColor = texture2D(terraintex, trnTexCoord);"<<endl;
	//theVS<<"	vec4 zvalue = gl_FrontColor;"<<endl;

	//theVS<<"	vec4 tmpVertex = gl_Vertex;"<<endl;
	//
	//theVS<<"	gl_Vertex.z = zvalue.z*256.0+zvalue.w;"<<endl;
	//
	//theVS<<"	if(gl_Vertex.z < -500.0)"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_Vertex.z  = -1.0;"<<endl;		
	//theVS<<"	}"<<endl;

	////ocean color blue
	//theVS<<"	if(gl_Vertex.z<0.0 && gl_Vertex.z>-2.0 )"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_FrontColor.r = 0.0;"<<endl;
	//theVS<<"		gl_FrontColor.g = 0.0;"<<endl;
	//theVS<<"		gl_FrontColor.b = 1.0;"<<endl;	
	//theVS<<"	}"<<endl;	

	////low place
	//theVS<<"	if(tmpVertex.z>0.0 && tmpVertex.z<2000.0 )"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_FrontColor.r = tmpVertex.z/2000.0;"<<endl;
	//theVS<<"		gl_FrontColor.g = gl_FrontColor.r;"<<endl;
	//theVS<<"		gl_FrontColor.b = 0.0;"<<endl;	
	//theVS<<"	}"<<endl;

	////hight place
	//theVS<<"	if(tmpVertex.z>2000.0 && tmpVertex.z<6000.0 )"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_FrontColor.r = 1.0-(tmpVertex.z-2000.0)/8000.0;"<<endl;
	//theVS<<"		gl_FrontColor.g = gl_FrontColor.r;"<<endl;
	//theVS<<"		gl_FrontColor.b = 0.0;"<<endl;	
	//theVS<<"	}"<<endl;

	////above the ice line
	//theVS<<"	if(tmpVertex.z>6000.0)"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_FrontColor.r = tmpVertex.z/9000.0;"<<endl;
	//theVS<<"		gl_FrontColor.g = tmpVertex.z/9000.0;"<<endl;
	//theVS<<"		gl_FrontColor.b = tmpVertex.z/9000.0;"<<endl;	
	//theVS<<"	}"<<endl;

	//theVS<<"	gl_Vertex.z = gl_Vertex.z/9000.0;"<<endl;	

	theVS<<"	gl_Vertex.z = sin(3.0*gl_Vertex.x)+cos(3.0*gl_Vertex.y);"<<endl;	
	theVS<<"	xpos = gl_Vertex.x;"<<endl;
	theVS<<"	ypos = gl_Vertex.y;"<<endl;
	//theVS<<"	vec4 tmpVertex = gl_Vertex;"<<endl;
	//tmpVertex.z = zvalue.w
    
	theVS<<"	gl_Position = ftransform();"<<endl;
	//theVS<<"	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"<<endl;
	//theVS<<"	normalize(zvalue);"<<endl;
	//theVS<<"	gl_FrontColor.r = zvalue.x;"<<endl;
	////theVS<<"	xpos = gl_Position.x;"<<endl;
	////theVS<<"	ypos = gl_Position.y;"<<endl;
	theVS<<"}"<<endl<<ends;

	//------------------------------
	
	thePS<<"varying float xpos;"<<endl;
	thePS<<"varying float ypos;"<<endl;
	thePS<<"uniform sampler2D terraintex;"<<endl;

	thePS<<"void main()"<<endl;
	thePS<<"{"<<endl;

	thePS<<"	gl_FragColor = texture2D(terraintex, vec2(xpos, ypos));"<<endl;	

	//thePS<<"	if(gl_FragColor.r<0.1 && gl_FragColor.b<0.1 && gl_FragColor.b<0.1)"<<endl;
	//thePS<<"	{"<<endl;

	//thePS<<"		gl_FragColor = gl_Color;"<<endl;
	//thePS<<"	}"<<endl;
	thePS<<"}"<<endl<<ends;

}

void shaderCode_terrain_roam(std::ostrstream& theVS, std::ostrstream& thePS)
{
	theVS.clear();
	thePS.clear();

	theVS<<"uniform sampler2D terraintex;"<<endl;

	theVS<<"uniform float hightMax;"<<endl;
	theVS<<"uniform float renderSizex;"<<endl;
	theVS<<"uniform float renderSizey;"<<endl;
	theVS<<"uniform vec3 eyepos;"<<endl;

	theVS<<"varying float curHeight;"<<endl;

	theVS<<"void main()"<<endl;
	theVS<<"{"<<endl;
	theVS<<"	gl_FrontColor = gl_Color;"<<endl;

	theVS<<"	vec2 trnTexCoord = vec2(gl_Vertex.y/renderSizey,gl_Vertex.x/renderSizex);"<<endl;	
	//theVS<<"	float tmplen = length(trnTexCoord);"<<endl;
	//theVS<<"	trnTexCoord = vec2(trnTexCoord.x/tmplen, trnTexCoord.y/tmplen);"<<endl;
	theVS<<"	vec4 zvalue = texture2D(terraintex, trnTexCoord);"<<endl;
	//theVS<<"	vec4 zvalue = gl_FrontColor;"<<endl;

	theVS<<"	vec4 tmpVertex = gl_Vertex;"<<endl;
	//theVS<<"	tmpVertex.x = renderSizex - tmpVertex.x;"<<endl;

	theVS<<"	tmpVertex.z = zvalue.x;"<<endl;

	theVS<<"	curHeight = zvalue.x;"<<endl;

	//theVS<<"	if(trnTexCoord.x<0.25)"<<endl;	
	//theVS<<"		gl_Vertex.z = zvalue.x;"<<endl;
	//theVS<<"	if(trnTexCoord.x>0.25 && gl_Vertex.x<0.5)"<<endl;
	//theVS<<"		gl_Vertex.z = zvalue.y;"<<endl;
	//theVS<<"	if(trnTexCoord.x>0.5 && gl_Vertex.x<0.75)"<<endl;	
	//theVS<<"		gl_Vertex.z = zvalue.x;"<<endl;
	//theVS<<"	if(trnTexCoord.x>0.75)"<<endl;
	//theVS<<"		gl_Vertex.z = zvalue.w;"<<endl;

	theVS<<"	if(tmpVertex.z < -500.0)"<<endl;
	theVS<<"	{"<<endl;
	theVS<<"		tmpVertex.z  = -1.0;"<<endl;
	theVS<<"		curHeight  = -1.0;"<<endl;
	theVS<<"	}"<<endl;

	////ocean color blue
	//theVS<<"	if(tmpVertex.z<=0.0)"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_FrontColor.r = 0.0;"<<endl;
	//theVS<<"		gl_FrontColor.g = 0.0;"<<endl;
	//theVS<<"		gl_FrontColor.b = 1.0;"<<endl;	
	//theVS<<"	}"<<endl;	

	////low place
	//theVS<<"	if(tmpVertex.z>0.0 && tmpVertex.z<=1000.0 )"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_FrontColor.r = tmpVertex.z/2000.0;"<<endl;
	//theVS<<"		gl_FrontColor.g = 1.0-(gl_FrontColor.r);"<<endl;
	//theVS<<"		gl_FrontColor.b = 0.0;"<<endl;	
	//theVS<<"	}"<<endl;

	////hight place
	//theVS<<"	if(tmpVertex.z>1000.0 && tmpVertex.z<=5000.0 )"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_FrontColor.r = 0.5-((tmpVertex.z-1000.0)/10000.0);"<<endl;
	//theVS<<"		gl_FrontColor.g = gl_FrontColor.r;"<<endl;
	//theVS<<"		gl_FrontColor.b = 0.0;"<<endl;

	//theVS<<"	}"<<endl;

	////above the ice line
	//theVS<<"	if(tmpVertex.z>5000.0)"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_FrontColor.r = (tmpVertex.z-5000.0)/2000.0;"<<endl;
	//theVS<<"		gl_FrontColor.g = gl_FrontColor.r;"<<endl;
	//theVS<<"		gl_FrontColor.b = gl_FrontColor.r;"<<endl;

	//theVS<<"	}"<<endl;	

	theVS<<"	tmpVertex.z = tmpVertex.z/hightMax;"<<endl;	

	theVS<<"	float thedis = distance(tmpVertex.xyz, eyepos);"<<endl;

	theVS<<"	if(thedis<1.0)"<<endl;
	theVS<<"	{"<<endl;
	theVS<<"		curHeight  = -1.0;"<<endl;
	theVS<<"	}"<<endl;

	//theVS<<"	gl_FrontColor.g = gl_Vertex.z;"<<endl;

	//theVS<<"	gl_Position = ftransform();"<<endl;
	theVS<<"	gl_Position = gl_ModelViewProjectionMatrix * tmpVertex;"<<endl;

	theVS<<"}"<<endl<<ends;

	//------------------------------

	thePS<<"varying float xpos;"<<endl;
	thePS<<"varying float ypos;"<<endl;
	thePS<<"varying float curHeight;"<<endl;
	//thePS<<"uniform sampler2D terraintex;"<<endl;

	thePS<<"void main()"<<endl;
	thePS<<"{"<<endl;

	thePS<<"	gl_FragColor = gl_Color;"<<endl;

	//thePS<<"	gl_FragColor = texture2D(terraintex, vec2(xpos, ypos));"<<endl;	

	//thePS<<"	if(gl_FragColor.r<0.1 && gl_FragColor.b<0.1 && gl_FragColor.b<0.1)"<<endl;
	//thePS<<"	{"<<endl;

	//ocean color blue
	thePS<<"	if(curHeight<=0.0)"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		gl_FragColor.r = 0.0;"<<endl;
	thePS<<"		gl_FragColor.g = 0.0;"<<endl;
	thePS<<"		gl_FragColor.b = 1.0;"<<endl;	
	thePS<<"	}"<<endl;	

	//low place
	thePS<<"	if(curHeight>0.0 && curHeight<=1000.0 )"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		gl_FragColor.r = curHeight/2000.0;"<<endl;
	thePS<<"		gl_FragColor.g = 1.0-(gl_FragColor.r);"<<endl;
	thePS<<"		gl_FragColor.b = 0.0;"<<endl;	
	thePS<<"	}"<<endl;

	//hight place
	thePS<<"	if(curHeight>1000.0 && curHeight<=5000.0 )"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		gl_FragColor.r = 0.5-((curHeight-1000.0)/10000.0);"<<endl;
	thePS<<"		gl_FragColor.g = gl_FragColor.r;"<<endl;
	thePS<<"		gl_FragColor.b = 0.0;"<<endl;

	thePS<<"	}"<<endl;

	//above the ice line
	thePS<<"	if(curHeight>5000.0)"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		gl_FragColor.r = (curHeight-5000.0)/2000.0;"<<endl;
	thePS<<"		gl_FragColor.g = gl_FragColor.r;"<<endl;
	thePS<<"		gl_FragColor.b = gl_FragColor.r;"<<endl;

	thePS<<"	}"<<endl;

	//thePS<<"	}"<<endl;
	thePS<<"}"<<endl<<ends;

}


void shaderCode_terrain_detail(std::ostrstream& theVS, std::ostrstream& thePS)
{
	theVS.clear();
	thePS.clear();

	theVS<<"uniform sampler2D terraintex;"<<endl;

	theVS<<"uniform float hightMax;"<<endl;
	theVS<<"uniform float renderSizex;"<<endl;
	theVS<<"uniform float renderSizey;"<<endl;
	theVS<<"uniform vec3 eyepos;"<<endl;
	theVS<<"uniform int isDetail;"<<endl;

	theVS<<"varying float curHeight;"<<endl;
	theVS<<"varying vec2 curxy;"<<endl;
	theVS<<"varying vec2 relyxy;"<<endl;
    
	theVS<<"void main()"<<endl;
	theVS<<"{"<<endl;
	theVS<<"	gl_FrontColor = gl_Color;"<<endl;
	
	theVS<<"	vec4 tmpVertex = gl_Vertex;"<<endl;
	
	

	theVS<<"	vec4 zvalue = gl_Vertex;"<<endl;
	theVS<<"	relyxy = gl_Vertex.xy;"<<endl;
	theVS<<"	if(isDetail == 1)"<<endl;
	theVS<<"	{"<<endl;
	//theVS<<"		eyepos.xy = eyepos.yx;"<<endl;
	//theVS<<"		tmpVertex.x = gl_Vertex.x - eyepos.x + renderSizex;"<<endl;
	theVS<<"		tmpVertex.x = gl_Vertex.x + eyepos.x;"<<endl;
	theVS<<"		tmpVertex.y = gl_Vertex.y + eyepos.y;"<<endl;
	theVS<<"		vec2 trnTexCoord = vec2(tmpVertex.x/renderSizex,tmpVertex.y/renderSizey);"<<endl;	
	theVS<<"		zvalue = texture2D(terraintex, trnTexCoord);"<<endl;
	theVS<<"		tmpVertex.z = zvalue.x;"<<endl;
	theVS<<"	}"<<endl;
	//theVS<<"	else"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		tmpVertex.z = 10.0;"<<endl;
	//theVS<<"	}"<<endl;

		
	//theVS<<"	tmpVertex.x = renderSizex - tmpVertex.x;"<<endl;

	theVS<<"	curxy = tmpVertex.xy;"<<endl;	
	//theVS<<"	float tmplen = length(trnTexCoord);"<<endl;
	//theVS<<"	trnTexCoord = vec2(trnTexCoord.x/tmplen, trnTexCoord.y/tmplen);"<<endl;
	
	//-----------------------------------------hight sample
	//theVS<<"	vec4 zvalue = texture2D(terraintex, trnTexCoord);"<<endl;
	//theVS<<"	vec4 zvalue = gl_FrontColor;"<<endl;
	//theVS<<"	vec4 tmpVertex = gl_Vertex;"<<endl;
	//theVS<<"	tmpVertex.x = renderSizex - tmpVertex.x;"<<endl;
	
	
	theVS<<"	curHeight = tmpVertex.z;"<<endl;

	//theVS<<"	if(trnTexCoord.x<0.25)"<<endl;	
	//theVS<<"		gl_Vertex.z = zvalue.x;"<<endl;
	//theVS<<"	if(trnTexCoord.x>0.25 && gl_Vertex.x<0.5)"<<endl;
	//theVS<<"		gl_Vertex.z = zvalue.y;"<<endl;
	//theVS<<"	if(trnTexCoord.x>0.5 && gl_Vertex.x<0.75)"<<endl;	
	//theVS<<"		gl_Vertex.z = zvalue.x;"<<endl;
	//theVS<<"	if(trnTexCoord.x>0.75)"<<endl;
	//theVS<<"		gl_Vertex.z = zvalue.w;"<<endl;

	theVS<<"	if(tmpVertex.z < -500.0)"<<endl;
	theVS<<"	{"<<endl;
	theVS<<"		tmpVertex.z  = -1.0;"<<endl;
	theVS<<"		curHeight  = -1.0;"<<endl;
	theVS<<"	}"<<endl;

	//-----------------------------------------color set
	////ocean color blue
	//theVS<<"	if(tmpVertex.z<=0.0)"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_FrontColor.r = 0.0;"<<endl;
	//theVS<<"		gl_FrontColor.g = 0.0;"<<endl;
	//theVS<<"		gl_FrontColor.b = 1.0;"<<endl;	
	//theVS<<"	}"<<endl;	

	////low place
	//theVS<<"	if(tmpVertex.z>0.0 && tmpVertex.z<=1000.0 )"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_FrontColor.r = tmpVertex.z/2000.0;"<<endl;
	//theVS<<"		gl_FrontColor.g = 1.0-(gl_FrontColor.r);"<<endl;
	//theVS<<"		gl_FrontColor.b = 0.0;"<<endl;	
	//theVS<<"	}"<<endl;

	////hight place
	//theVS<<"	if(tmpVertex.z>1000.0 && tmpVertex.z<=5000.0 )"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_FrontColor.r = 0.5-((tmpVertex.z-1000.0)/10000.0);"<<endl;
	//theVS<<"		gl_FrontColor.g = gl_FrontColor.r;"<<endl;
	//theVS<<"		gl_FrontColor.b = 0.0;"<<endl;
	//theVS<<"	}"<<endl;

	////above the ice line
	//theVS<<"	if(tmpVertex.z>5000.0)"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_FrontColor.r = (tmpVertex.z-5000.0)/2000.0;"<<endl;
	//theVS<<"		gl_FrontColor.g = gl_FrontColor.r;"<<endl;
	//theVS<<"		gl_FrontColor.b = gl_FrontColor.r;"<<endl;
	//theVS<<"	}"<<endl;	

	theVS<<"	tmpVertex.z = tmpVertex.z/hightMax;"<<endl;	

	//theVS<<"	float thedis = distance(tmpVertex.xyz, eyepos);"<<endl;

	//theVS<<"	if(thedis<1.0)"<<endl;
	//theVS<<"	if(isDetail == 1)"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		curHeight  = -1.0;"<<endl;
	//theVS<<"	}"<<endl;

	//theVS<<"	gl_FrontColor.g = gl_Vertex.z;"<<endl;

	//theVS<<"	gl_Position = ftransform();"<<endl;
	//theVS<<"	tmpVertex.x = renderSizex-tmpVertex.x;"<<endl;
	
	theVS<<"	gl_Position = gl_ModelViewProjectionMatrix * tmpVertex;"<<endl;

	theVS<<"}"<<endl<<ends;

	//------------------------------

	thePS<<"uniform float renderSizex;"<<endl;
	thePS<<"uniform float renderSizey;"<<endl;
	thePS<<"uniform sampler2D colortex;"<<endl;
	thePS<<"uniform int isColorTex;"<<endl;
	thePS<<"uniform float crate;"<<endl;
	thePS<<"uniform int isDetail;"<<endl;

	thePS<<"varying float xpos;"<<endl;
	thePS<<"varying float ypos;"<<endl;
	thePS<<"varying float curHeight;"<<endl;	
	thePS<<"varying vec2 curxy;"<<endl;
	thePS<<"varying vec2 relyxy;"<<endl;

	thePS<<"void main()"<<endl;
	thePS<<"{"<<endl;

	thePS<<"	if(curxy.x<0.0 || curxy.x>renderSizex)"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		discard;"<<endl;
	thePS<<"	}"<<endl;

	thePS<<"	if(curxy.y<0.0 || curxy.y>renderSizey)"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		discard;"<<endl;
	thePS<<"	}"<<endl;

	thePS<<"	gl_FragColor = gl_Color;"<<endl;

	//thePS<<"	gl_FragColor = texture2D(terraintex, vec2(xpos, ypos));"<<endl;	

	//thePS<<"	if(gl_FragColor.r<0.1 && gl_FragColor.b<0.1 && gl_FragColor.b<0.1)"<<endl;
	//thePS<<"	{"<<endl;

	//ocean color blue
	thePS<<"	if(curHeight<=0.0)"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		gl_FragColor.r = 0.0;"<<endl;
	thePS<<"		gl_FragColor.g = 0.0;"<<endl;
	thePS<<"		gl_FragColor.b = 1.0;"<<endl;	
	thePS<<"	}"<<endl;	

	//low place
	thePS<<"	if(curHeight>0.0 && curHeight<=1000.0 )"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		gl_FragColor.r = curHeight/2000.0;"<<endl;
	thePS<<"		gl_FragColor.g = 1.0-(gl_FragColor.r);"<<endl;
	thePS<<"		gl_FragColor.b = 0.0;"<<endl;	
	thePS<<"	}"<<endl;

	//hight place
	thePS<<"	if(curHeight>1000.0 && curHeight<=5000.0 )"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		gl_FragColor.r = 0.5-((curHeight-1000.0)/10000.0);"<<endl;
	thePS<<"		gl_FragColor.g = gl_FragColor.r;"<<endl;
	thePS<<"		gl_FragColor.b = 0.0;"<<endl;
	thePS<<"	}"<<endl;

	//above the ice line
	thePS<<"	if(curHeight>5000.0)"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		gl_FragColor.r = (curHeight-5000.0)/2000.0;"<<endl;
	thePS<<"		gl_FragColor.g = gl_FragColor.r;"<<endl;
	thePS<<"		gl_FragColor.b = gl_FragColor.r;"<<endl;
	thePS<<"	}"<<endl;	

	thePS<<"	if(isColorTex == 1)"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		vec4 tmpcolor = texture2D(colortex,vec2(curxy.x/renderSizex,curxy.y/renderSizey));"<<endl;
	thePS<<"		gl_FragColor = mix(gl_FragColor, tmpcolor, vec4(crate,crate,crate,crate));"<<endl;
	thePS<<"	}"<<endl;

	thePS<<"	float lentoeye;"<<endl;
	thePS<<"	if(isDetail == 1)"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		lentoeye = length(relyxy);"<<endl;
	thePS<<"		if(lentoeye<0.3)"<<endl;
	thePS<<"		{"<<endl;
	thePS<<"			gl_FragColor.a=1.0;"<<endl;
	//thePS<<"			gl_FragColor.r=1.0;"<<endl;
	//thePS<<"			gl_FragColor.g=0.0;"<<endl;
	//thePS<<"			gl_FragColor.b=0.0;"<<endl;
	thePS<<"		}"<<endl;
	thePS<<"		else if (lentoeye>0.5)"<<endl;
	thePS<<"		{"<<endl;
	thePS<<"			gl_FragColor.a=0.0;"<<endl;
	//thePS<<"			gl_FragColor.r=0.0;"<<endl;
	//thePS<<"			gl_FragColor.g=0.0;"<<endl;
	//thePS<<"			gl_FragColor.b=1.0;"<<endl;
	thePS<<"		}"<<endl;
	thePS<<"		else"<<endl;
	thePS<<"		{"<<endl;
	thePS<<"			gl_FragColor.a=(0.5-lentoeye)/0.2;"<<endl;
	//thePS<<"			gl_FragColor.r=gl_FragColor.a;"<<endl;
	//thePS<<"			gl_FragColor.g=0.0;"<<endl;
	//thePS<<"			gl_FragColor.b=1.0-gl_FragColor.a;"<<endl;
	thePS<<"		}"<<endl;
	thePS<<"	}"<<endl;
	
	//thePS<<"	}"<<endl;
	thePS<<"}"<<endl<<ends;

}


void shaderCode_terrain_1(std::ostrstream& theVS, std::ostrstream& thePS)
{
	theVS<<"uniform sampler2D terraintex;"<<endl;

	theVS<<"uniform float hightMax;"<<endl;

	theVS<<"varying float curHeight;"<<endl;


	theVS<<"void main()"<<endl;
	theVS<<"{"<<endl;
	theVS<<"	gl_FrontColor = gl_Color;"<<endl;

	theVS<<"	vec2 trnTexCoord = vec2(1.0-gl_Vertex.x/3.0, gl_Vertex.y/3.0);"<<endl;	
	//theVS<<"	float tmplen = length(trnTexCoord);"<<endl;
	//theVS<<"	trnTexCoord = vec2(trnTexCoord.x/tmplen, trnTexCoord.y/tmplen);"<<endl;
	theVS<<"	float zvalue = texture2D(terraintex, trnTexCoord);"<<endl;
	//theVS<<"	zvalue += texture2D(terraintex, vec2(trnTexCoord.x+0.001,trnTexCoord.y));"<<endl;
	//theVS<<"	zvalue += texture2D(terraintex, vec2(trnTexCoord.x-0.001,trnTexCoord.y));"<<endl;
	//theVS<<"	zvalue += texture2D(terraintex, vec2(trnTexCoord.x,trnTexCoord.y+0.001));"<<endl;
	//theVS<<"	zvalue += texture2D(terraintex, vec2(trnTexCoord.x,trnTexCoord.y-0.001));"<<endl;
	//theVS<<"	zvalue = zvalue/5.0;"<<endl;
	//theVS<<"	vec4 zvalue = gl_FrontColor;"<<endl;

	//theVS<<"	vec4 tmpVertex = gl_Vertex;"<<endl;

	theVS<<"	gl_Vertex.z = zvalue;"<<endl;
	
	//theVS<<"	if(trnTexCoord.x<0.25)"<<endl;	
	//theVS<<"		gl_Vertex.z = zvalue.x;"<<endl;
	//theVS<<"	if(trnTexCoord.x>0.25 && gl_Vertex.x<0.5)"<<endl;
	//theVS<<"		gl_Vertex.z = zvalue.y;"<<endl;
	//theVS<<"	if(trnTexCoord.x>0.5 && gl_Vertex.x<0.75)"<<endl;	
	//theVS<<"		gl_Vertex.z = zvalue.x;"<<endl;
	//theVS<<"	if(trnTexCoord.x>0.75)"<<endl;
	//theVS<<"		gl_Vertex.z = zvalue.w;"<<endl;
	
	theVS<<"	if(gl_Vertex.z < -500.0)"<<endl;
	theVS<<"	{"<<endl;
	theVS<<"		gl_Vertex.z  = -1.0;"<<endl;		
	theVS<<"	}"<<endl;

	theVS<<"curHeight = gl_Vertex.z;"<<endl;	

	////ocean color blue
	//theVS<<"	if(gl_Vertex.z<0.0 && gl_Vertex.z>-2.0 )"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_FrontColor.r = 0.0;"<<endl;
	//theVS<<"		gl_FrontColor.g = 0.0;"<<endl;
	//theVS<<"		gl_FrontColor.b = 1.0;"<<endl;	
	//theVS<<"	}"<<endl;	

	////low place
	//theVS<<"	if(gl_Vertex.z>0.0 && gl_Vertex.z<1000.0 )"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_FrontColor.r = gl_Vertex.z/2000.0;"<<endl;
	//theVS<<"		gl_FrontColor.g = 0.7-(gl_Vertex.z/2000.0);"<<endl;
	//theVS<<"		gl_FrontColor.b = 0.1;"<<endl;	
	//theVS<<"	}"<<endl;

	////hight place
	//theVS<<"	if(gl_Vertex.z>1000.0 && gl_Vertex.z<6000.0 )"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_FrontColor.r = 0.5-((gl_Vertex.z-999.0)/11000.0);"<<endl;
	//theVS<<"		gl_FrontColor.g = 0.2-((gl_Vertex.z-999.0)/21000.0);"<<endl;
	//theVS<<"		gl_FrontColor.b = 0.1;"<<endl;
	//
	//theVS<<"	}"<<endl;

	////above the ice line
	//theVS<<"	if(gl_Vertex.z>6000.0)"<<endl;
	//theVS<<"	{"<<endl;
	//theVS<<"		gl_FrontColor.r = gl_Vertex.z/8500.0;"<<endl;
	//theVS<<"		gl_FrontColor.g = gl_Vertex.z/8500.0;"<<endl;
	//theVS<<"		gl_FrontColor.b = gl_Vertex.z/8500.0;"<<endl;
	//
	//theVS<<"	}"<<endl;	

	theVS<<"	gl_Vertex.z = gl_Vertex.z/hightMax;"<<endl;	

	//theVS<<"	gl_FrontColor.g = gl_Vertex.z;"<<endl;

	theVS<<"	gl_Position = ftransform();"<<endl;
	//theVS<<"	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"<<endl;

	theVS<<"}"<<endl<<ends;

	//------------------------------

	//thePS<<"varying float xpos;"<<endl;
	//thePS<<"varying float ypos;"<<endl;
	thePS<<"varying float curHeight;"<<endl;
	//thePS<<"uniform sampler2D terraintex;"<<endl;

	thePS<<"void main()"<<endl;
	thePS<<"{"<<endl;

	thePS<<"	if(curHeight<0.0 && curHeight>-2.0 )"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		gl_FragColor.r = 0.0;"<<endl;
	thePS<<"		gl_FragColor.g = 0.0;"<<endl;
	thePS<<"		gl_FragColor.b = 1.0;"<<endl;	
	thePS<<"	}"<<endl;	

	//low place
	thePS<<"	if(curHeight>0.0 && curHeight<1000.0 )"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		gl_FragColor.r = curHeight/2000.0;"<<endl;
	thePS<<"		gl_FragColor.g = 0.7-(curHeight/2000.0);"<<endl;
	thePS<<"		gl_FragColor.b = 0.1;"<<endl;	
	thePS<<"	}"<<endl;

	//hight place
	thePS<<"	if(curHeight>1000.0 && curHeight<6000.0 )"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		gl_FragColor.r = 0.5-((curHeight-999.0)/11000.0);"<<endl;
	thePS<<"		gl_FragColor.g = 0.2-((curHeight-999.0)/21000.0);"<<endl;
	thePS<<"		gl_FragColor.b = 0.1;"<<endl;	
	thePS<<"	}"<<endl;

	//above the ice line
	thePS<<"	if(curHeight>6000.0)"<<endl;
	thePS<<"	{"<<endl;
	thePS<<"		gl_FragColor.r = curHeight/8500.0;"<<endl;
	thePS<<"		gl_FragColor.g = curHeight/8500.0;"<<endl;
	thePS<<"		gl_FragColor.b = curHeight/8500.0;"<<endl;
	thePS<<"	}"<<endl;


	//thePS<<"		gl_FragColor = gl_Color;"<<endl;
	//thePS<<"	}"<<endl;
	thePS<<"}"<<endl<<ends;

}


bool shaderSet_terrian()
{
	std::ostrstream theVS;	
	std::ostrstream thePS;

	shaderCode_terrain_detail(theVS, thePS);

	//1 of  3 in compile stage
	
	AppGloble::theVShader = glCreateShader(GL_VERTEX_SHADER);
	AppGloble::thePShader = glCreateShader(GL_FRAGMENT_SHADER);

	const char * vv = theVS.str();
	const char * pp = thePS.str();

	//2 of  3 in compile stage
	glShaderSource(AppGloble::theVShader, 1, &vv, NULL);
	glShaderSource(AppGloble::thePShader, 1, &pp, NULL);

	//3 of  3 in compile stage
	glCompileShader(AppGloble::theVShader);

	int param;
	glGetShaderiv(AppGloble::theVShader, GL_COMPILE_STATUS, &param); 

	if(param!=GL_TRUE)
	{
		cout<<endl<<"fail to compile vs shader"<<endl;
		cout<<endl<<"the shader is:"<<endl<<theVS.str()<<endl;

		char errorInf[1024*5] = {'\0'};
		int theErrorReportLen;
		glGetShaderInfoLog(AppGloble::theVShader, 1024*5, &theErrorReportLen, errorInf); 

		cout<<endl<<"the Error Report is:"<<endl<<errorInf<<endl;

		return false;
	}

	glCompileShader(AppGloble::thePShader);

	glGetShaderiv(AppGloble::thePShader, GL_COMPILE_STATUS, &param); 
	if(param!=GL_TRUE)
	{
		cout<<endl<<"fail to compile ps shader"<<endl;
		cout<<endl<<"the shader is:"<<endl<<thePS.str();

		char errorInf[1024*5] = {'\0'};
		int theErrorReportLen;
		glGetShaderInfoLog(AppGloble::thePShader, 1024*5, &theErrorReportLen, errorInf); 

		cout<<endl<<"the Error Report is:"<<endl<<errorInf<<endl;

		return false;
	}

	//1 of  3 in link stage
	AppGloble::prgrm = glCreateProgram();

	//2 of  3 in link stage
	glAttachShader(AppGloble::prgrm, AppGloble::theVShader);
	glAttachShader(AppGloble::prgrm, AppGloble::thePShader);	

	//3 of  3 in link stage
	glLinkProgram(AppGloble::prgrm);

	glGetProgramiv(AppGloble::prgrm, GL_LINK_STATUS, &param); 	
	if(param!=GL_TRUE)
	{
		cout<<endl<<"fail to link shader code"<<endl;	

		cout<<endl<<"========================the vs shader is:"<<endl<<theVS.str();

		cout<<endl<<endl<<"=============================the ps shader is:"<<endl<<thePS.str()<<endl;

		char errorInf[1024*5] = {'\0'};
		int theErrorReportLen;
		glGetProgramInfoLog(AppGloble::prgrm, 1024*5, &theErrorReportLen, errorInf); 

		cout<<endl<<"the Error Report is:"<<endl<<errorInf<<endl;

		return false;
	}

	glUseProgram(AppGloble::prgrm);

	//Uniform attatching
	AppGloble::terrainTexloc = glGetUniformLocation(AppGloble::prgrm, "terraintex"); 
	
	glGetProgramiv(AppGloble::prgrm, GL_LINK_STATUS, &param); 	
	if(param!=GL_TRUE)
	{
		cout<<endl<<"fail to attatching Uniform value"<<endl;
	
		char errorInf[1024*5] = {'\0'};
		int theErrorReportLen;
		glGetProgramInfoLog(AppGloble::prgrm, 1024*5, &theErrorReportLen, errorInf); 

		cout<<endl<<"the Error Report is:"<<endl<<errorInf<<endl;

		return false;
	}

	glUniform1i(AppGloble::terrainTexloc, 0);


	AppGloble::colorTexloc = glGetUniformLocation(AppGloble::prgrm, "colortex"); 

	glGetProgramiv(AppGloble::prgrm, GL_LINK_STATUS, &param); 	
	if(param!=GL_TRUE)
	{
		cout<<endl<<"fail to attatching Uniform value"<<endl;

		char errorInf[1024*5] = {'\0'};
		int theErrorReportLen;
		glGetProgramInfoLog(AppGloble::prgrm, 1024*5, &theErrorReportLen, errorInf); 

		cout<<endl<<"the Error Report is:"<<endl<<errorInf<<endl;

		return false;
	}

	glUniform1i(AppGloble::colorTexloc, 1);

	AppGloble::maxHightloc = glGetUniformLocation(AppGloble::prgrm, "hightMax"); 

	glGetProgramiv(AppGloble::prgrm, GL_LINK_STATUS, &param); 	
	if(param!=GL_TRUE)
	{
		cout<<endl<<"fail to attatching Uniform value"<<endl;


		char errorInf[1024*5] = {'\0'};
		int theErrorReportLen;
		glGetProgramInfoLog(AppGloble::prgrm, 1024*5, &theErrorReportLen, errorInf); 

		cout<<endl<<"the Error Report is:"<<endl<<errorInf<<endl;

		return false;
	}

	glUniform1f(AppGloble::maxHightloc, AppGloble::maxHightValue);

	AppGloble::crateLoc = glGetUniformLocation(AppGloble::prgrm, "crate"); 
	glUniform1f(AppGloble::crateLoc, AppGloble::crateValue);	

	AppGloble::isDetailloc = glGetUniformLocation(AppGloble::prgrm, "isDetail"); 
	//glUniform1i(AppGloble::isDetailloc, AppGloble::isDetail);

	AppGloble::isColorTexUseloc = glGetUniformLocation(AppGloble::prgrm, "isColorTex"); 
	glUniform1i(AppGloble::isColorTexUseloc, AppGloble::isColorTexUse);

	AppGloble::renderSizexLoc = glGetUniformLocation(AppGloble::prgrm, "renderSizex"); 
	glUniform1f(AppGloble::renderSizexLoc, AppGloble::renderSizex);

	AppGloble::renderSizeyLoc = glGetUniformLocation(AppGloble::prgrm, "renderSizey"); 
	glUniform1f(AppGloble::renderSizeyLoc, AppGloble::renderSizey);

	AppGloble::eyeposloc = glGetUniformLocation(AppGloble::prgrm, "eyepos"); 
	glUniform3f(AppGloble::renderSizeyLoc,
		AppGloble::noweye.eyex,
		AppGloble::noweye.eyey,
		AppGloble::noweye.eyez);


	return true;
}